package com.ly.common.core.utils;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.ly.common.core.constant.BaseConstant;
import com.ly.common.core.constant.TokenConstants;
import com.ly.common.core.exception.ServiceException;
import com.ly.common.core.tool.security.config.properties.TokenProperties;
import com.ly.common.core.tool.security.entity.LyUser;
import com.ly.common.core.tool.security.utils.TokenUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * emp模块工具类
 *
 * @author luoyun
 * @Date: 2025/5/13 16:33
 * @Note:
 */
public class BaseUtils {

    private static TokenProperties tokenProperties = SpringUtils.getBean(TokenProperties.class);
    private static RedisUtils redisUtils = SpringUtils.getBean(RedisUtils.class);

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        HttpServletRequest request =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        HttpServletResponse response =
                ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        return response;
    }

    /**
     * 获取用户身份信息
     *
     * @desc 通过头部token信息获取
     */
    public static LyUser getLoginUser() {
        String uuid = BaseUtils.getUuidByToken(null);
        LyUser user = (LyUser) redisUtils.get(TokenConstants.LOGIN_TOKEN_KEY + uuid);
        if (user == null) {
            throw new ServiceException("找不到用户信息");
        }
        return user;
    }

    /**
     * 根据Token值获取uuid
     * @param token
     * @return
     */
    public static String getUuidByToken(String token){
        if(token == null){
            // 获取请求携带的令牌
            HttpServletRequest request =
                    ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            token = request.getHeader(tokenProperties.getHeader());
        }
        if (StrUtil.isEmpty(token)) {
            throw new ServiceException("token为空");
        }
        if (!token.startsWith(tokenProperties.getTokenPrefix())) {
            throw new ServiceException("token格式错误");
        }
        token = token.replaceFirst(tokenProperties.getTokenPrefix(), "");
        JSONObject claims = TokenUtils.parseToken(token);
        // 解析对应的权限以及用户信息
        String uuid = claims.getStr(TokenConstants.LOGIN_USER_KEY);
        return uuid;
    }

    /**
     * 获取工号
     *
     * @return
     */
    public static Boolean checkIsAdmin() {
        LyUser vo = BaseUtils.getLoginUser();
        return "admin".equals(vo.getUsername());
    }

    /**
     * 租户权限
     *
     * @return
     */
    public static Boolean checkTenantAuth() {
        LyUser vo = BaseUtils.getLoginUser();
        return BaseConstant.TENANT_FLAG && "admin".equals(vo.getUsername());
    }

    /**
     * 获取工号
     *
     * @return
     */
    public static String getWorkNum() {
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getWorkNo();
    }

    /**
     * 获取组织信息
     *
     * @return
     */
    public static String getOrgCode() {
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getOrgCode();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static String getId() {
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getId();
    }

    /**
     * 获取用户名
     *
     * @return
     */
    public static String getUsername() {
        if (RequestContextHolder.getRequestAttributes() == null) {
            return "-1";
        }
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getUsername();
    }

    /**
     * 获取用户名
     *
     * @return
     */
    public static String getRealname() {
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getRealname();
    }

    /**
     * 获取负责部门
     *
     * @return
     */
    public static String getDeptId() {
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getDeptId();
    }

    /**
     * 获取负责部门
     *
     * @return
     */
    public static String getDeptIds() {
        LyUser vo = BaseUtils.getLoginUser();
        return vo.getDeptIds();
    }

    /**
     * 获取租户
     * todo:应该是列表
     *
     * @return
     */
    public static Integer getTenant() {
        LyUser vo = BaseUtils.getLoginUser();
        // return vo.getTenant();
        return ObjUtil.isNotNull(vo.getTenant()) ? vo.getTenant() : 1000;
    }

    /**
     * 获取Suitunit信息
     *
     * @return
     */
    public static String getSuitUnit() {
        String suitUnit = "05010000";
        return suitUnit;
    }
}
